import { ROUTE_MODULE_LEVEL, type RouteMetaResolveRaw } from "@/utils";
import { useRoute } from "vue-router";

/** 应用菜单数据分发 */
export const useMenusDataDispatch = ({
  getMenus,
  getMenuFlatList,
  getExtractLevel1ToHeader,
}: {
  /** 菜单树 */
  getMenus: () => RouteMetaResolveRaw[];
  /** 扁平化菜单列表 */
  getMenuFlatList: () => RouteMetaResolveRaw[];
  /** 判断是否提取1级路由模块到HEADER */
  getExtractLevel1ToHeader: () => boolean;
}) => {
  /** 菜单树 */
  const menus = computed(getMenus);

  /** 扁平化菜单列表 */
  const menuFlatList = computed(getMenuFlatList);

  /** 是否提取1级路由模块到Header */
  const extractLevel1ToHeader = computed(getExtractLevel1ToHeader);

  const route = useRoute();

  /** 当前路由的聚焦菜单 */
  const activeMenu = computed(() => {
    return menuFlatList.value.find((item) => {
      return item.path === route.path;
    });
  });

  /** 当前聚焦菜单需要重定向路由的子菜单 */
  const needRedirectToFirstChildMenu = computed(
    () =>
      activeMenu.value && getCanRedirectFirstChildMenu(activeMenu.value, true),
  );

  /** 当前聚焦的模块路径 */
  const activeModulePath = computed(() => {
    if (!activeMenu.value) {
      console.warn(
        `当前 ${route.path} 未找到聚焦的菜单 可能路由配置不展示菜单`,
      );
      return;
    }
    // 若是1级菜单 模块路径即是自身路径
    if (activeMenu.value.routeLevel === ROUTE_MODULE_LEVEL) {
      return activeMenu.value.path;
    } else {
      return activeMenu.value.parentPathList[0];
    }
  });

  /** 当前聚焦的模块对应的菜单 */
  const activeModuleMenu = computed(() => {
    return activeModulePath.value
      ? menus.value.find((item) => item.path === activeModulePath.value)
      : undefined;
  });

  /** 当前聚焦的模块对应菜单的子菜单树 */
  const activeModuleMenuChildrenMenu = computed(() => {
    return activeModuleMenu.value?.children;
  });

  /** sidebar菜单 */
  const sidebarMenus = computed(() => {
    return extractLevel1ToHeader.value
      ? activeModuleMenuChildrenMenu.value || []
      : menus.value;
  });

  /** 路由[模块级别]列表 */
  const routeModuleLevelList = computed<RouteMetaResolveRaw[]>(() => {
    return menus.value.filter((item) => item.routeLevel === ROUTE_MODULE_LEVEL);
  });

  /** 路由[模块级别]列表屏蔽children值 */
  const routeModuleLevel1ListNoChildren = computed(() =>
    routeModuleLevelList.value.map((item) => {
      return {
        ...item,
        // 避免 展示子菜单
        children: [],
      };
    }),
  );

  /** header菜单 */
  const headerMenus = computed(() => {
    return extractLevel1ToHeader.value
      ? routeModuleLevel1ListNoChildren.value
      : [];
  });

  /** 获取可以重定向的第一个子菜单 */
  const getCanRedirectFirstChildMenu = (
    menu: RouteMetaResolveRaw,
    /** 是否是递归的初始调用 */
    isInitCall: boolean,
  ): RouteMetaResolveRaw | undefined => {
    if (menu.redirectToFirstChildRoute) {
      const firstChild = menu.children[0];
      if (firstChild) {
        return getCanRedirectFirstChildMenu(firstChild, false);
      }
    }
    // 需要的是子菜单 非初始化才保底返回递归入参
    if (!isInitCall) {
      return menu;
    }
  };

  return {
    sidebarMenus,
    headerMenus,
    activeMenu,
    activeModuleMenu,
    needRedirectToFirstChildMenu,
  };
};
